/*
 * Created on 2004/09/27
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui;



import java.io.File;





/**
 * 
 *
 */
public abstract class AbstractBasicFileApplication {
private boolean needSave;
private File file;

private String fileExtension;

public String getFileExtension() {
    return fileExtension;
}
public void setFileExtension(String defaultExtension) {
    this.fileExtension = defaultExtension;
}
public String fixFileExtension(String path){
    if(getFileExtension()==null){
        return path;
    }else{
        if(path.toLowerCase().endsWith("."+getFileExtension().toLowerCase())){
            return path;
        }else{
            return path+"."+getFileExtension();
        }
    }
}
public String getDefaultFileName() {
    return defaultFileName;
}
public void setDefaultFileName(String defaultFileName) {
    this.defaultFileName = defaultFileName;
}
public File getFile() {
    return file;
}
public void setFile(File file) {
    this.file = file;
}
public String getFileSaveTitle() {
    return fileSaveTitle;
}
public void setFileSaveTitle(String fileSaveTitle) {
    this.fileSaveTitle = fileSaveTitle;
}
public String getHeadFileSaveMessage() {
    return headFileSaveMessage;
}
public void setHeadFileSaveMessage(String headFileSaveMessage) {
    this.headFileSaveMessage = headFileSaveMessage;
}

public String getTailFileSaveMessage() {
    return tailFileSaveMessage;
}
public void setTailFileSaveMessage(String tailFileSaveMessage) {
    this.tailFileSaveMessage = tailFileSaveMessage;
}
protected String defaultFileName=Messages.getString("noname");
protected String headFileSaveMessage=Messages.getString("file");
protected String tailFileSaveMessage=Messages.getString("isnt_saved_are_you");
protected String fileSaveTitle=Messages.getString("confirm_filesave");

public static final int YES=0;
public static final int NO=1;
public static final int CANCEL=2;

public boolean isNeedSave(){
    return needSave;
}
public void setNeedSave(boolean bool){
    boolean old=needSave;
    
    this.needSave=bool;
    
    if(needSave!=old){
        saveStatusChanged();
    }
}

public void openFile(File currentFile){
    if(isNeedSave()){
        int result=askSave();
        if(result==YES){
        if(!saveFile()){
            return; //cancel
        }
        }else if(result==CANCEL){
            return;
        }
    }
    
    
    if(currentFile!=null){
        if(isStandardFile(currentFile)){
        file=currentFile;
        execOpen();
        needSave=false;//new file.
        }else{
        execOpenSubFile(currentFile);    
        }
        if(file!=null){
            needSave=false;//some execOpen action.remove file.
        	}
        }
    saveStatusChanged();
}
public void openFile(){
    if(isNeedSave()){
        int result=askSave();
        if(result==YES){
        if(!saveFile()){
            return; //cancel
        }
        }else if(result==CANCEL){
            return;
        }
    }
    
    File currentFile=execChooseOpenFile();
    if(currentFile!=null){
        if(isStandardFile(currentFile)){
        file=currentFile;
        execOpen();
        needSave=false;//new file.
        }else{
        execOpenSubFile(currentFile);    
        }
        if(file!=null){
            needSave=false;//some execOpen action.remove file.
        	}
        }
    saveStatusChanged();
}

/*
 * open sub file.
 * handle file yourself.
 */
protected void execOpenSubFile(File file){
    
}

protected boolean isStandardFile(File file){
    return true;
}
/**
 * 
 */
protected abstract void execOpen();
protected abstract File execChooseOpenFile();
/**
 * 
 */
protected abstract void execSave();

/*
 * change title bar and etc.
 */
protected abstract void saveStatusChanged();
protected abstract File execChooseSaveFile();
public String getFileName(){
    if(file==null){
        return defaultFileName;
    }else{
        return file.getName();
    }
}



public int askSave(){
    return executeAskSave(fileSaveTitle,headFileSaveMessage+getFileName()+tailFileSaveMessage);
}
protected abstract int executeAskSave(String title,String message);

public boolean saveFile(){
if(file==null){
        file=execChooseSaveFile();
    }
    if(file!=null){
        execSave();
        
        needSave=false;
        saveStatusChanged();
        return true;
    }else{
        return false;
    }
}

public boolean saveAsFile(){
    File tmpFile=execChooseSaveFile();
    if(tmpFile!=null){
        file=tmpFile;
        execSave();
        
        needSave=false;
        saveStatusChanged();
        return true;
    }else{
        return false;
    }
}

public void newFile(){

    
    if(isNeedSave()){
        int result=askSave();
        if(result==YES){
        if(!saveFile()){
            return; //cancel
        }
        }else if(result==CANCEL){
            return;
        }
    }
    
    
    execNew();
    file=null;
    needSave=false;
    saveStatusChanged();
}

/**
 * 
 */
protected abstract void execNew();
/*
public void exit(){
    if(isNeedSave()){
        int result=askSave();
        if(result==YES){
        if(!saveFile()){
            return; //cancel
        }
        }else if(result==CANCEL){
            return;
        }
    }
    execExit();
}*/

/**
 * 
 */
protected abstract void execExit();

}
